<template>
	<view>
		<!-- 搜索输入框 -->
		<view class="search-box">
			<uni-search-bar @confirm="search" @blur="blur" @input="input" :radius="100" cancelButton="none"
				@click="gotoDetail"></uni-search-bar>

		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-if="searchList.length==0">
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="deleteHistory"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">

				<uni-tag :text="item" v-for="(item,index) in history" :key="index"
					@click="historyGoodsDetail(item)"></uni-tag>
			</view>

		</view>
		<!-- 搜索展示 列表-->
		<view class="search-list-box" v-else>
			<view class="search-item" v-for="(item,index) in searchList" :key="index"
				@click="gotoDetail(item.goods_id)">
				<view class="search-item-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchList: [],
				historyList: []

			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},

		methods: {


			input(e) {

				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				}, 500)

			},
			async getSearchList() {
				if (this.kw === '') {
					this.searchList = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) uni.$showMsg("数据获取失败")
				this.searchList = res.message
				this.saveSearchList()
			},
			gotoDetail(goods_id) {
				uni.navigateTo({
					url: '/subpackage/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			historyGoodsDetail(item) {

				uni.navigateTo({
					url: '/subpackage/goods_detail/goods_detail?query=' + item
				})
			},
			saveSearchList() {

				// this.historyList.push(this.kw)

				const set = new Set(this.historyList)
				//让新的搜索记录成为最后一条
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			deleteHistory() {
				this.historyList = []
				uni.clearStorageSync()
			}
		},
		computed: {
			history() {

				return [...this.historyList].reverse()
			}
		}

	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 10;

	}

	.search-item {
		height: 40px;
		margin-top: 10px;
		display: flex;
		align-items: center;
		border-bottom: solid 1px #efefef;


	}


	.search-item-name {
		font-size: 16px;
		// 文字不换行
		white-space: nowrap;
		//文字溢出隐藏
		overflow: hidden;
		//溢出用省略号替代
		text-overflow: ellipsis;
		margin-right: 3px;
	}

	.history-title {
		display: flex;
		justify-content: space-between;
		border: 1px solid #efefef;
		height: 40px;
		line-height: 50px;
	}


	.history-list {
		display: flex;
		flex-wrap: wrap;


	}

	.uni-tag {
		background-color: #efefef;
		margin-top: 5px;
		margin-right: 5px;
	}
</style>